package com.ruoyi.ipms_crm.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ipms_crm.dto.ContractDetailsDto;
import com.ruoyi.ipms_crm.entity.IpmsContract;
import com.ruoyi.ipms_crm.service.IpmsContractService;
import com.ruoyi.system.api.entity.Inventor;
import com.ruoyi.system.api.patent.RemotePatentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 合同Controller
 *
 * @author ruoyi
 * @date 2022-04-21
 */
@RestController
@RequestMapping("/crm/contract")
public class IpmsContractController extends BaseController
{
    @Autowired
    private IpmsContractService ipmsContractService;

    /**
     * 查询合同列表
     */
    //@RequiresPermissions("system:contract:list")
    @GetMapping("/list")
    public TableDataInfo list(IpmsContract ipmsContract)
    {
        startPage();
        List<IpmsContract> list = ipmsContractService.selectIpmsContractList(ipmsContract);
        return getDataTable(list);
    }

    /**
     * 导出合同列表
     */
    //@RequiresPermissions("system:contract:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, IpmsContract ipmsContract)
    {
        List<IpmsContract> list = ipmsContractService.selectIpmsContractList(ipmsContract);
        ExcelUtil<IpmsContract> util = new ExcelUtil<IpmsContract>(IpmsContract.class);
        util.exportExcel(response, list, "合同数据");
    }

    /**
     * 获取合同详细信息
     */
    //@RequiresPermissions("system:contract:query")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Integer contractId)
    {
        return AjaxResult.success(ipmsContractService.selectIpmsContractByContractId(contractId));
    }

    /**
     * 新增合同
     */
    // @RequiresPermissions("system:contract:add")
    @PostMapping
    public AjaxResult add(@RequestBody IpmsContract ipmsContract)
    {
        return toAjax(ipmsContractService.insertIpmsContract(ipmsContract));
    }

    /**
     * 修改合同
     */
    //@RequiresPermissions("system:contract:edit")

    @PutMapping
    public AjaxResult edit(@RequestBody IpmsContract ipmsContract)
    {
        return toAjax(ipmsContractService.updateIpmsContract(ipmsContract));
    }

    /**
     * 删除合同
     */
	@DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Integer[] contractIds)
    {
        return toAjax(ipmsContractService.deleteIpmsContractByContractIds(contractIds));
    }

    @Resource
    private RemotePatentService remotePatentService;

    /*新增发明人*/
    @PostMapping("/addinventor")
    public  AjaxResult feignAddOne(@RequestBody Inventor inventor){
        return remotePatentService.addOne(inventor);
    }

    /*合同详情*/
    @PostMapping("/details")
    public AjaxResult getDetails(@RequestBody IpmsContract contract){
        ContractDetailsDto detailsDto = ipmsContractService.getDetails(contract.getContractId());
        return  detailsDto != null? AjaxResult.success("合同详情",detailsDto):AjaxResult.error("查找错误");
    }


    /**
     * 案件编号获取合同费用
     */
    @GetMapping("/cost/{caseNo}")
    public TableDataInfo costInfo(@PathVariable("caseNo") String caseNo)
    {
        startPage();
        return getDataTable(ipmsContractService.selectContractByCaseNo(caseNo));
    }
}
